using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ImdbServices;
using System.Collections;

namespace MyCollections_V1.Forms
{
    public partial class fFindImdbMovies : Form
    {
        static Imdb imdb = new Imdb();
        static string[] resultCodes;
        static Movie[] resultMovies;

        public fFindImdbMovies()
        {
            InitializeComponent();

            this.bt_search_in_imdb.Visible = true;
            this.bt_search_in_imdb.Enabled = false;
            this.bt_show_details.Visible = false;
            this.bt_show_details.Enabled = false;

            // IMDB
            imdb.SearchResultsDownloaded +=
            new Imdb.SearchResultsDownloadedEventHandler(SearchResultsDownloaded);
            imdb.MovieInfoDownloaded +=
                new Imdb.MovieInfoDownloadedEventHandler(MovieInfoDownloadedEventHandler);
        }

        private void search_in_imdb_visible()
        {
            this.bt_search_in_imdb.Visible = true;
            this.bt_search_in_imdb.Enabled = true;
            this.bt_show_details.Visible = false;
            this.bt_show_details.Enabled = false;
        }

        private void show_details_visible()
        {
            this.bt_search_in_imdb.Visible = false;
            this.bt_search_in_imdb.Enabled = false;
            this.bt_show_details.Visible = true;
            this.bt_show_details.Enabled = true;
        }

        private void bt_search_Click(object sender, EventArgs e)
        {
            bool results = false;

            if (String.IsNullOrEmpty(this.tb_title.Text))
                return;

            // TODO : find movie 
            imdb.SearchMovieAsync(this.tb_title.Text, true, true);
        }

        #region IMDB

        private void MovieInfoDownloadedEventHandler(Movie M)
        {
            /*lbl_title.Text = M.Title;
            lbl_year.Text = Convert.ToString(M.Year);
            lbl_genres.Text = "";
            foreach (string item in M.Genres)
            {
                lbl_genres.Text += item + ",";
            }
            lbl_rating.Text = M.Rating.ToString();
            txt_plot.Text = M.Description;

            if (M.ImageURL != "")
            {
                pictureBox1.Image =
                    Image.FromStream(
                    new System.IO.MemoryStream(
                        new System.Net.WebClient().DownloadData(M.ImageURL)));
            }*/
        }

        private void SearchResultsDownloaded(ImdbServices.MoviesResultset M)
        {
            //NOTE: this object Movie in this results countains only the title and code

            int totalResults = M.ExactMatches.Count + M.PartialMatches.Count + M.PopularTitles.Count;
            resultCodes = new string[totalResults];
            ArrayList movies = new ArrayList();

            foreach (Movie movie in M.ExactMatches)
            {
                if(movie != null)
                    movies.Add(movie);
            }

            foreach (Movie movie in M.PopularTitles)
            {
                if (movie != null)
                    movies.Add(movie);
            }

            foreach (Movie movie in M.PartialMatches)
            {
                if (movie != null)
                    movies.Add(movie);
            }

            foreach (Movie movie in movies)
            {
                this.checkedListBox.Items.Add(movie.Title);
            }
        }
        #endregion
    }
}